package base.core.log;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import lombok.Data;
import lombok.experimental.Accessors;

/**
 * @Description debug打印自定义日志
 * @Author Pengwei
 * @CreatDate 2021/12/24 16:39
 */
public class Mark {
    private static final ThreadLocal<List<Point>> THREAD_LOCAL = new ThreadLocal<>();

    static void start(String title,String content) {
        List<Point> pointList = new ArrayList<>();
        THREAD_LOCAL.set(pointList);
        LocalDateTime markTime = LocalDateTime.now();
        Point point = new Point().setTitle(title).setContent(content).setMarkTime(markTime).setDuration(0);
        pointList.add(point);
    }

    public static void mark(String title,String content) {
        LocalDateTime markTime = LocalDateTime.now();
        List<Point> pointList = THREAD_LOCAL.get();
        Point latest = pointList.get(pointList.size() - 1);
        Point point = new Point().setTitle(title).setContent(content).setMarkTime(markTime).setDuration(Duration.between(latest.getMarkTime(), markTime).toMillis());
        pointList.add(point);
    }

    public static void mark(String title) {
        mark(title,"");
    }

    /**
     * 统计本次debug的详细信息
     */
     static List<Point> marks() {
       return THREAD_LOCAL.get();
    }

    @Data
    @Accessors(chain = true)
    static class Point {
        long duration;
        String title;
        String content;
        LocalDateTime markTime;
    }

}
